package com.xudow.app.ui;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.Toast;

import com.activeshare.edu.ucenter.models.user.UserProfileWithOther;
import com.xudow.app.R;
import com.xudow.app.XApplication;
import com.xudow.app.ui.adapter.StudentAdapter;
import com.xudow.app.ui.task.OrderStudentAddTask;
import com.xudow.app.ui.task.OrderStudentSelectTask;
import com.xudow.app.ui.task.StudentListTask;
import com.xudow.app.util.Maps;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;

/**
 * 为订单选择学生
 *
 * @author lukecheng
 */
public class OrderStudentSelectActivity extends BaseActivity implements AdapterView.OnItemClickListener {

    private Long orderId; //订单ID

    private StudentListTask studentListTask;

    private ListView studentsListView;
    private ProgressBar progressBar;

    private StudentAdapter studentAdapter;
    private List<UserProfileWithOther> students;

    private OrderStudentSelectTask orderStudentSelectTask;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_order_student_select);

        orderId = getIntent().getLongExtra("orderId", 0L);

        progressBar = (ProgressBar) findViewById(R.id.progressbar);

        studentsListView = (ListView) findViewById(R.id.students);
        studentsListView.setOnItemClickListener(this);

        XApplication application = (XApplication) getApplication();
        application.addChainActivity(this);
    }

    @Override
    protected void onResume() {
        super.onResume();

        studentListTask = new StudentListTask(this, studentListTaskHandler);
        studentListTask.execute();
    }

    private Handler studentListTaskHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            if (StudentListTask.SUCCESS == msg.what) {
                UserProfileWithOther[] studentArr = (UserProfileWithOther[]) msg.getData()
                        .getSerializable("students");

                if (null == studentArr || studentArr.length <=0) {
                    progressBar.setVisibility(View.GONE);
                    return;
                }

                students = new ArrayList<UserProfileWithOther>();
                students.addAll(Arrays.asList(studentArr));

                studentAdapter = new StudentAdapter(OrderStudentSelectActivity.this,
                        R.layout.item_student, students);

                progressBar.setVisibility(View.GONE);
                studentsListView.setVisibility(View.VISIBLE);

                studentsListView.setAdapter(studentAdapter);
                studentsListView.setOnItemClickListener(OrderStudentSelectActivity.this);
            } else {
                String message = getString(R.string.loading_fail);
                Toast.makeText(OrderStudentSelectActivity.this, message, Toast.LENGTH_SHORT).show();
            }
        }
    };

    public void onBackBtnClick(View view) {
        finish();
    }

    /**
     * 新增学生按钮点击事件
     *
     * @param view
     */
    public void addNewStudent(View view) {
        Intent intent = new Intent(this, UserRelativeActivity.class);
        intent.putExtra("orderId", orderId);
        startActivity(intent);
    }

    private Handler orderStudentSelectTaskHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            hideLoadingDialog();

            if (OrderStudentAddTask.SUCCESS == msg.what) {
                String message = getString(R.string.order_student_select_success);
                Toast.makeText(OrderStudentSelectActivity.this, message, Toast.LENGTH_SHORT).show();

                finish();
            } else {
                String message = getString(R.string.process_fail);
                Toast.makeText(OrderStudentSelectActivity.this, message, Toast.LENGTH_SHORT).show();
            }
        }
    };


    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        final Long userProfileId = students.get(position).getId();

        String message = getString(R.string.order_student_select_confirm);
        AlertDialog dlg = new AlertDialog.Builder(this)
                .setMessage(message)
                .setPositiveButton(getString(R.string.confirm), new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        showLodingDialog(getString(R.string.processing));

                        HashMap<String, String> params = Maps.newHashMap();
                        params.put("student_id", "" + userProfileId);
                        params.put("order_id", "" + orderId);

                        orderStudentSelectTask = new OrderStudentSelectTask(OrderStudentSelectActivity.this, orderStudentSelectTaskHandler);
                        orderStudentSelectTask.execute(params);
                    }
                })
                .setNegativeButton(getString(R.string.cancel), new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                    }
                }).create();
        dlg.show();
    }
}
